﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthBar : MonoBehaviour
{
    public Vector3 offset;
    public const float damage = 10f;
    public float health = 100f;

    public Transform onwer;
    private SpriteRenderer healthRender;
    

    // Start is called before the first frame update
    void Start()
    {
        healthRender = transform.GetChild(1).GetComponent<SpriteRenderer>();
        //Debug.Log(healthRender.name);
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = onwer.transform.position + offset;
    }

    public float takeDamage(float damage = damage)
    {
        health = Mathf.Clamp(health - damage, 0, 100);
        healthRender.material.color = Color.Lerp(Color.red, Color.green, health * 0.01f);
        healthRender.transform.localScale = new Vector3(health * 0.01f, 1, 1);
        return health;
    }
}
